﻿using RantopadService.Business;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services.Description;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RantopadService.query
{
    public partial class QueryAdmins : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Session["logid"].ToString() != "admin")//只有admin可以管理别人的账号
                ButAddOrUpdate.Enabled = false;
            BindData();
        }
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
            BindData();
        }

        private void BindData()
        {
            string wherelike = "";
            if (this.Session["logid"].ToString() != "admin") //只有admin可以管理别人的账号
                wherelike=this.Session["logid"].ToString();
            var data = AdminBusiness.getAllAdmins(wherelike);
            GridView1.DataSource = data;
            GridView1.DataBind();
            LabelMsg.Text = string.Format("共{0}条数据", data.Rows.Count);
        }
        private void BindData(string wherelike)
        {
            if (this.Session["logid"].ToString() != "admin")//只有admin可以管理别人的账号
                wherelike = this.Session["logid"].ToString();
            var data = AdminBusiness.getAllAdmins(wherelike);
            GridView1.DataSource = data;
            GridView1.DataBind();
            LabelMsg.Text = string.Format("共{0}条数据",data.Rows.Count);
           
        }

        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {

        }

        protected void Search_Click(object sender, EventArgs e)
        {
            BindData(TextWhereLike.Text.ToString());
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            //GridView1.EditIndex = -1;
            //GridView1.DataBind();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            //GridView1.EditIndex = e.NewEditIndex;
            //GridView1.DataBind();
            
            //var name = GridView1.DataKeys[e.].Value.ToString();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var name =GridView1.DataKeys[e.RowIndex].Value.ToString();
            if (this.Session["logid"].ToString() != "admin")//只有admin可以删除账号
            {
                Response.Write("<script>alert('没有权限，删除失败！')</script>");
                return;
            }
            if (AdminBusiness.delAdminByName(name) > 0)
                Response.Write("<script>alert('删除成功！')</script>");
            else
                Response.Write("<script>alert('删除失败！')</script>");
            BindData();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow) 
            { 
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate) 
            { 
            ((LinkButton)e.Row.Cells[3].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除：\"" + e.Row.Cells[0].Text + "\"吗?')"); 
            } 
            } 
        }

        protected void ButAddOrUpdate_Click(object sender, EventArgs e)
        {
            if(chechAdmin())
            {
                int success = 0;

                if (ButAddOrUpdate.Text == "添加") success = AdminBusiness.addAdmin( AdminName.Text, MD5Helper.GetMd5Str( Psd.Text));

                if (ButAddOrUpdate.Text == "修改") success = AdminBusiness.updateAdmin( AdminName.Text, MD5Helper.GetMd5Str( Psd.Text));

                if(success>0)
                {
                    BindData();
                    Msg.Text = ButAddOrUpdate.Text + "成功";
                }
                else
                {
                    Msg.Text = ButAddOrUpdate.Text + "失败";
                }

            }
        }

        private bool chechAdmin()
        {
            Msg.Text = "";
            if(AdminName.Text == ""||Psd.Text == ""||rPsd.Text == "")
            {
                Msg.Text = "信息不全";
                return false;
            }
            if (Psd.Text != rPsd.Text)
            {
                Msg.Text = "两次密码不一样";
                return false;
            }
            return true;
        }

        protected void ButClear_Click(object sender, EventArgs e)
        {
            
            AdminName.Text = "";
            Psd.Text = "";
            rPsd.Text = "";
            ButAddOrUpdate.Text = "添加";
            AdminName.Enabled = true;
            ButAddOrUpdate.Enabled = false;
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "EditAdmin")
            {
                // Convert the row index stored in the CommandArgument
                // property to an Integer.
                int index = Convert.ToInt32(e.CommandArgument);

                // Get the last name of the selected author from the appropriate
                // cell in the GridView control.
                GridViewRow selectedRow = GridView1.Rows[index];
                //TableCell contactName = selectedRow.Cells[1];
                //string contact = contactName.Text;
                //Msg.Text = contact;

                
                AdminName.Text = selectedRow.Cells[0].Text;

                ButAddOrUpdate.Text = "修改";
                ButAddOrUpdate.Enabled = true;
                AdminName.Enabled = false;
               
                

            }
        } 
    }
}